import axios from 'axios'
import {Toast} from 'mint-ui'
import router from './router'
import Vue from 'vue'
import {logout} from "./utils/tool"

// 超时时间
axios.defaults.timeout = 5000

// http request 请求拦截器
axios.interceptors.request.use(
    config => {
        let token = sessionStorage.getItem('token')
        if (token) {
            config.headers.token = sessionStorage.getItem('token')
        }
        return config
    },
    error => {
        Toast('链接超时');
        return Promise.reject(error)
    });

// http response 响应拦截器
axios.interceptors.response.use(
    response=> {
        if (response.data.code === 2) {
            logout()
            router.replace({name: 'login'})
            Toast(response.data.msg)
        }
        return response
    },
    error => {
        return Promise.reject(error)   // 返回接口返回的错误信息
    });

Vue.prototype.$http = axios

// export default axios